package com.vitabuybuy.tv.ui.mine.talent.usercenter

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.ImageView
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import com.luck.picture.lib.PictureSelector
import com.library.common.BuildConfig
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityAddEditCaseBinding
import com.vitabuybuy.tv.dialog.OptionsDialog
import com.vitabuybuy.tv.dto.CaseBean
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.library.common.net.NetUrl
import com.library.common.toast.XToast
import com.library.common.util.PictureUtils
import com.vitabuybuy.tv.adaptebinding.ImageAdapter
import com.vitabuybuy.tv.dto.BannerDto
import com.vitabuybuy.tv.ui.mine.talent.usercenter.viewmodel.UserCenterViewModel
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.android.synthetic.main.activity_add_edit_case.*

class EditOrAddNewCaseActivity : BaseActivity<UserCenterViewModel, ActivityAddEditCaseBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_add_edit_case

    private var caseBean: CaseBean? = null
    private var REQUEST_CODE_CAMMER = 0x11
    private val picDialog by lazy { OptionsDialog(mContext) }

    private var caseImage = ""

    override fun onGetBundle(bundle: Bundle?) {
        caseBean = bundle?.getSerializable("caseBean") as? CaseBean
    }

    override fun initView(savedInstanceState: Bundle?) {
        mTvRight.apply {
            visibility = View.VISIBLE
            text = getString(R.string.confirm)
        }
        if (caseBean == null) {
            setTitle(getString(R.string.add_new_case))
        } else {
            setTitle(getString(R.string.edit_case))
            etEdit.setText(caseBean?.content)
            caseImage = caseBean?.image!!
            ImageAdapter.setNomalImageUrl(
                ivImage, caseImage,
                ContextCompat.getDrawable(mContext, R.drawable.default_image)
            )
        }
    }

    override fun eventHandling() {
        ivImage.setOnClickListener {
            AndPermission.with(this)
                .runtime()
                .permission(Permission.Group.CAMERA, Permission.Group.STORAGE)
                .onDenied {
                    if (AndPermission.hasAlwaysDeniedPermission(this, it)) {
                        // 这些权限被用户总是拒绝。
                        XToast.normal(getString(R.string.request_necessary_permissions))
                    }
                }
                .onGranted {
                    picDialog.show()
                }.start()
        }
        picDialog.onTwoClick = {
            PictureUtils.openAluamOneRectangle(mContext, REQUEST_CODE_CAMMER)
        }
        picDialog.onOneClick = {
            PictureUtils.openCameraRectangle(mContext, REQUEST_CODE_CAMMER)
        }
        mTvRight.setOnClickListener {
            when {
                etEdit.text.isEmpty() -> XToast.normal(getString(R.string.pls_describe_case))
                caseImage.isEmpty() -> XToast.normal(getString(R.string.pls_upload_case_image))
                else -> {
                    if (caseBean != null) {
                        viewModel.resetCase(caseBean?.id, etEdit.text.toString(), caseImage)
                    } else {
                        viewModel.addCase(etEdit.text.toString(), caseImage)
                    }
                }
            }
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) return
        when (requestCode) {
            REQUEST_CODE_CAMMER -> {
                val path = PictureSelector.obtainMultipleResult(data)[0].cutPath
                viewModel.uploadImage(path)
            }
        }
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            1 -> {
                if (caseBean != null) {
                    XToast.success(getString(R.string.update_success))
                } else {
                    XToast.success(getString(R.string.add_success))
                }
                finish()
            }
            2 -> {
                caseImage = NetUrl.FILE_PROFILE.plus(msg.obj as String)
                ImageAdapter.setNomalImageUrl(
                    ivImage,
                    caseImage,
                    ContextCompat.getDrawable(mContext, R.drawable.default_image)
                )
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, caseBean: CaseBean?) {
            mContext.startActivity(
                bundleOf(Pair("caseBean", caseBean)),
                EditOrAddNewCaseActivity::class.java
            )
        }
    }
}
